'use strict';

/**
 * @ngdoc service
 * @name jgappApp.TaskReviewService
 * @description
 * # TaskReviewService
 * Service in the jgappApp.
 */
angular.module('jgappApp')
    .service('TaskReviewService', function($http, Util) {
        // AngularJS will instantiate a singleton by calling "new" on this function
        var TaskReview = function(options) {
            this.result = {};
            this.getData(options);
        };
        TaskReview.prototype.getData = function(options) {
            $http.get(options.url, {
                params: options.param
            }).
            success(function(data, status, headers, config) {
                if (data.logout) {
                    Util.logout();
                    return;
                }
                if (data.success) {
                    // 补充taskid和operationId两个字段
                    var param = {};
                    param.taskId = data.result.task && data.result.task.id;
                    param.operations = data.result.operations;
                    param.currentObjectForm = data.result.currentObjectForm;
                    // 并行审批需要固定的按钮，如果后台没有返回按钮就按照通过和不通过返回
                    var operations = [{
                        "id": 1,
                        "name": "通过",
                    }, {
                        "id": 0,
                        "name": "不通过",
                    }];
                    // 如果是并行审批或者是自定义流程审批的话operationid用上面的替换
                    if (param.currentObjectForm == "syncAduit"||param.currentObjectForm=="caduit") {
                        param.operations = operations
                    }
                    if (param.operations) {
                        param.operations.forEach(function(item, index) {
                            if (item.name == "通过") {
                                item.picScr = "images/icon_pass.png";
                                item.className = "c-succ";
                            } else if (item.name == "不通过") {
                                item.picScr = "images/icon_unpass.png";
                                item.className = "c-red";
                            }
                        });

                    }
                    var key = data.result.formKey || data.result.currentObjectName;
                    this.result = angular.extend(data.result[key], param);
                    if (options.callback) {
                        options.callback(this.result, key);
                    }
                } else {
                    Util.Toast(data.result, {
                        callback: Util.goBack
                    });
                }
            }.bind(this)).
            error(function(data, status, headers, config) {}.bind(this));
        };
        return TaskReview;

    });
